package org.xurui.sxl.protocol.command.impl;

import org.jetlinks.core.message.DeviceMessage;
import org.jetlinks.core.message.codec.FromDeviceMessageContext;
import org.jetlinks.core.message.event.EventMessage;
import org.xurui.sxl.protocol.command.CmdCodec;
import org.xurui.sxl.protocol.enums.CommandEnum;
import org.xurui.sxl.protocol.utils.BinaryUtils;
import org.xurui.sxl.protocol.utils.NumberUtils;
import org.xurui.sxl.protocol.utils.SxlMessageUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SOS 报警协议
 */
public class CmdCodec_SOS implements CmdCodec {
    @Override
    public boolean supportsCmd(String cmd) {
        return CommandEnum.SOS.name().equals(cmd);
    }

    @Override
    public boolean supportsRelayCmd() {
        return false;
    }

    @Override
    public List<DeviceMessage> parsePayload(String payload, FromDeviceMessageContext context) {
        //SOS 报警协议 设备上报 TODO 没有 SV,BAT
        //格式：*XX,YYYYYYYYYYYYYYY,SOS,HHMMSS,S,latitude,D,longitude,G,speed,pdop,MCC,MNC,LAC,CELLID,DDMMYY,track_status#
        //例子：*TJ,2020916012,SOS,130305,A,2212.8745,N,11346.6574,E,14.28,2.8,460,0,25106,12172,220912,FFFFFFFF#
        //回复格式：*XX,YYYYYYYYYYYYYYY,V4,SOS,seq,HHMMSS#
        //回复示例：*TJ,2020916012,V4,SOS,130305,130306#
        //参数说明：GPS 信息 S,latitude,D,longitude,G,speed,pdop 详见第三部分表格
        //参数说明：基站信息 MCC,MNC,LAC,CELLID 详见第一部分 VI 基站消息
        //解析数据为事件上报
        EventMessage eventMessage = new EventMessage();
        //ReportPropertyMessage report = ReportPropertyMessage.create();
        String[] payloadArr = payload.replaceAll("\\*", "").split(",");
        String deviceId = payloadArr[1];
        eventMessage.setEvent(CommandEnum.SOS.name());
        eventMessage.setDeviceId(deviceId);
        eventMessage.setMessageId(deviceId + "_" + payloadArr[3]);
        eventMessage.setTimestamp(System.currentTimeMillis());
        Map<String, Object> properties = new HashMap<>();
        properties.put("uptime",payloadArr[3]);
        properties.put("s",payloadArr[4]);
        properties.put("latitude",BinaryUtils.getWgs84LonLat(payloadArr[5],"lat"));
        properties.put("d",payloadArr[6]);
        properties.put("longitude",BinaryUtils.getWgs84LonLat(payloadArr[7],"lon"));
        properties.put("g",payloadArr[8]);
        properties.put("speed",NumberUtils.parseDouble(payloadArr[9]));
        properties.put("pdop",NumberUtils.parseDouble(payloadArr[10]));
        properties.put("mcc",payloadArr[11]);
        properties.put("mnc",payloadArr[12]);
        properties.put("lac",payloadArr[13]);
        properties.put("cellid",payloadArr[14]);
        properties.put("upday",payloadArr[15]);
        String trackerStatusHex = payloadArr[16];

        Map<String,Object> trackerStatusMap = parseTrackerStatus(trackerStatusHex);
        properties.put("trackerstatus",trackerStatusMap);
        //report.setProperties(properties);
        eventMessage.setData(properties);
        //回复设备数据
        //TODO 暂时没找到设备回复消息的方法
//        String day= BinaryUtils.getDateFormat();
//        String reply="*"+payloadArr[0]+","+deviceId+",V4,"+CommandEnum.SOS.name()+","+payloadArr[3]+","+day+"#";
//        context.getSession()
//            .send(EncodedMessage
//                .simple(Unpooled.wrappedBuffer(reply.getBytes())))
//            .subscribe();
        //return genDeviceMessage(report);
        SxlMessageUtils.bulidAndSend2DeviceV4Msg(context,payloadArr[0],deviceId,CommandEnum.SOS.name(),payloadArr[3]);
        return genDeviceMessage(eventMessage);
    }
}
